package com.ler.yrmall.order.vo;

import com.ler.yrmall.coupon.dto.CouponDTO;
import com.ler.yrmall.member.dto.MemberReceiveAddressDTO;
import com.ler.yrmall.order.entity.OrderItemEntity;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Leron
 */
@Data
public class OrderConfirmVO implements Serializable {

    private String orderToken;

    private MemberReceiveAddressDTO defaultAddress;

    private List<OrderItemEntity> items;

    private Integer count;

    private CouponDTO coupon;

    /**
     * 订单总金额
     */
    private BigDecimal totalAmount;

    /**
     * 实际支付金额
     */
    private BigDecimal payAmount;

    /**
     * get 序列化方法会通过调用 get 方法为序列化字段赋值
     * @return
     */
    public Integer getCount() {
        Integer i = 0;
        if (items != null) {
            for (OrderItemEntity item : items) {
                i += item.getSkuQuantity();
            }
        }
        return i;
    }

    public BigDecimal getTotalAmount() {
        return this.computeAmount();
    }

    public BigDecimal computeAmount() {
        BigDecimal sum = BigDecimal.ZERO;
        if (items != null) {
            for (OrderItemEntity item : items) {
                BigDecimal multiply = item.getSkuPrice().multiply(new BigDecimal(item.getSkuQuantity().toString()));
                sum = sum.add(multiply);
            }
        }
        return sum;
    }

    public BigDecimal getPayAmount() {
        if (this.coupon != null) {
            return this.computeAmount().subtract(this.coupon.getAmount());
        }
        return this.computeAmount();
    }

}
